package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.eo.SearchResumesEO;
import com.imooc.mo.HrCollectResumeMO;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.CountsService;
import com.imooc.service.ResumeSearchService;
import com.imooc.utils.LocalDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liansen
 * @create 06-20-0:01
 */
@RestController
@RequestMapping("/counts")
public class CountsController extends BaseInfoProperties {

    @Autowired
    private CountsService countsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ElasticsearchRestTemplate esTemplate;

    @Autowired
    private ResumeSearchService resumeSearchService;

    /**************************HR收藏简历start**********************************/

    /**
     * hr收藏简历
     * @return
     */
    @PostMapping("/addCollectResume")
    public GraceJSONResult addCollectResume(String hrId, String resumeExpectId){
        countsService.addCollect(hrId, resumeExpectId);
        return GraceJSONResult.ok();
    }

    /**
     * hr移除简历
     * @return
     */
    @PostMapping("/removeCollectResume")
    public GraceJSONResult removeCollectResume(String hrId, String resumeExpectId){
        countsService.removeCollect(hrId, resumeExpectId);
        return GraceJSONResult.ok();
    }

    /**
     * 判断简历有没有被hr收藏
     */
    @PostMapping("/isHrCollectResume")
    public GraceJSONResult isHrCollectResume(String hrId, String resumeExpectId){
        return GraceJSONResult.ok(countsService.isHrCollectResume(hrId, resumeExpectId));
    }

    /**
     * 查询hr收藏的简历总数
     */
    public GraceJSONResult getCollectResumeCounts(String hrId){
        String countsStr = redisTemplate.opsForValue().get(HR_COLLECT_RESUME_COUNTS + ":" + hrId);
        int counts = 0;
        if(StringUtils.isNotBlank(countsStr)){
            counts = Integer.parseInt(countsStr);
        }
        return GraceJSONResult.ok(counts);
    }

    /**
     * 查询hr收藏的简历的列表
     * 之前将用户的简历都放在了es中,查的时候得先从mongodb中查询hr收藏的简历id,
     * 再通过简历id去es中查询简历即可
     */
    public GraceJSONResult pagedCollectResumeList(String hrId, Integer page, Integer pageSize){
        //从mongodb中查询hr收藏的简历
        List<HrCollectResumeMO> list = countsService.queryPagedCollectResumeList(hrId, page, pageSize);
        List<String> resumeExpectIds = list.stream().map(HrCollectResumeMO::getResumeExpectId).collect(Collectors.toList());
        //获取每个简历的期望id，去es中查询对应的简历信息
        List<SearchResumesEO> resumesEOList = resumeSearchService.searchCollectResumes(resumeExpectIds);
        //SearchResumesEO中的hr收藏简历的时间就是HrCollectResumeMO中的创建时间
        for (SearchResumesEO resumesEO : resumesEOList) {
            for (HrCollectResumeMO collectResumeMO : list) {
                if(collectResumeMO.getResumeExpectId().equalsIgnoreCase(resumesEO.getResumeExpectId())){
                    resumesEO.setHrCollectResumeTime(LocalDateUtils.format(collectResumeMO.getCreateTime(), LocalDateUtils.DATETIME_PATTERN));
                }
            }
        }

        return GraceJSONResult.ok(resumesEOList);
    }


    /**************************HR收藏简历end**********************************/



}
